package com.ipzoe.web.controller.oms;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ipzoe.mem.service.IMemberService;
import com.ipzoe.oms.domain.Order;
import com.ipzoe.oms.service.IOrderService;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.core.page.TableDataInfo;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.common.utils.StringUtils;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.web.convert.OrderConvert;
import com.ipzoe.web.model.oms.OrderQuery;
import com.ipzoe.web.model.oms.OrderVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 订单Controller
 * 
 * @author eric
 * @date 2020-09-06
 */
@RestController
@RequestMapping("/oms/order" )
public class OrderController extends BaseController {

    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private IMemberService iMemberService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('oms:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderQuery order)
    {
        startPage();
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<Order>();
        if (StringUtils.isNotBlank(order.getOrderSn())){
            lqw.eq(Order::getOrderSn ,order.getOrderSn());
        }
        if (order.getMemberId() != null){
            lqw.eq(Order::getMemberId ,order.getMemberId());
        }
        if (order.getCardId() != null){
            lqw.eq(Order::getCardId ,order.getCardId());
        }
        if (StringUtils.isNotBlank(order.getCardNum())){
            lqw.eq(Order::getCardNum ,order.getCardNum());
        }
        if (order.getDealerId() != null){
            lqw.eq(Order::getDealerId ,order.getDealerId());
        }
        if (order.getPackageId() != null){
            lqw.eq(Order::getPackageId ,order.getPackageId());
        }
        if (order.getTotalAmount() != null){
            lqw.eq(Order::getTotalAmount ,order.getTotalAmount());
        }
        if (order.getPayAmount() != null){
            lqw.eq(Order::getPayAmount ,order.getPayAmount());
        }
        if (order.getPayType() != null){
            lqw.eq(Order::getPayType ,order.getPayType());
        }
        if (order.getSourceType() != null){
            lqw.eq(Order::getSourceType ,order.getSourceType());
        }
        if (order.getStatus() != null){
            lqw.eq(Order::getStatus ,order.getStatus());
        }
        if (StringUtils.isNotBlank(order.getNote())){
            lqw.eq(Order::getNote ,order.getNote());
        }
        if (order.getPaymentTime() != null){
            lqw.eq(Order::getPaymentTime ,order.getPaymentTime());
        }
        if (order.getOrderType() != null){
            lqw.eq(Order::getOrderType ,order.getOrderType());
        }
        lqw.apply(StrUtil.isNotEmpty(order.getCreateTimeBegin()),"date_format(create_time,'%y%m%d') >= date_format({0},'%y%m%d')",order.getCreateTimeBegin());
        lqw.apply(StrUtil.isNotEmpty(order.getCreateTimeEnd()),"date_format(create_time,'%y%m%d') <= date_format({0},'%y%m%d')",order.getCreateTimeEnd());
        lqw.orderByDesc(Order::getCreateTime);
        List<Order> list = iOrderService.list(lqw);
        TableDataInfo tableDataInfo = getDataTable(list);
        List<OrderVO> voList = OrderConvert.INSTANCE.convert(list);
        for (OrderVO orderVO : voList) {
            orderVO.setMember(iMemberService.getById(orderVO.getMemberId()));
        }
        tableDataInfo.setRows(voList);
        return tableDataInfo;
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('oms:order:export')" )
    @Log(title = "订单" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public AjaxResult export(Order order) {
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<Order>(order);
        List<Order> list = iOrderService.list(lqw);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order. class);
        return util.exportExcel(list, "order" );
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('oms:order:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iOrderService.getById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('oms:order:add')" )
    @Log(title = "订单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        return toAjax(iOrderService.save(order) ? 1 : 0);
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('oms:order:edit')" )
    @Log(title = "订单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(iOrderService.updateById(order) ? 1 : 0);
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('oms:order:remove')" )
    @Log(title = "订单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
